<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.dao.${className}Dao" > 
<!-- Result Map-->
<resultMap id="${className}ResultMap" type="${packageName}.vo.${className}" >
<#list attrs as a>
	<result column="${a.columnName}" property="${a.columnName}"/>
</#list>
</resultMap>

<sql id="base_sql">
<#list attrs as a> 
	${a.columnName},
</#list>
rownum nu
</sql>

<sql id="page_sql_before">
	select * from (
</sql>
<sql id="page_sql_after">
	<![CDATA[
	) a where a.nu>${r'${start}'} and a.nu<=${r'${end}'}
	]]>
</sql>

<!-- 查询条件 -->
<sql id="Base_Where_Sql">
	<where>
		<#list attrs as a>
			<if test="${a.columnName} != null and ${a.columnName} != ''">
				AND ${a.columnName} = ${r'${'}${a.columnName}${r'}'}
			</if>
		</#list>
	</where>
</sql>

<select id="query"  resultMap="${className}ResultMap" parameterType="Object">
	<include refid="page_sql_before"></include>
		select 
		<include refid="base_sql"></include> 
		from ${tableName}
		<include refid="Base_Where_Sql"></include>
	<include refid="page_sql_after"></include>
</select>

<select id="queryById" resultMap="${tableName}ResultMap" parameterType="Object">
	<include refid="base_sql"></include> 
		from ${tableName}
		where 
		<#list attrs as a>
			<#if a.primary?contains("TRUE")>
				<if test="${a.columnName} != null and ${a.columnName} != ''">
					${a.columnName} = ${r'${'}${a.columnName}${r'}'}
				</if>
			</#if> 
		</#list>
</select>

<delete id="delete" parameterType="Object">
	delete ${tableName} 
	<include refid="Base_Where_Sql"></include>
</delete>

<insert id="insert" parameterType="Object">
	insert into ${tableName}(
		<#list attrs as a>
			<if test="${a.columnName} != null and ${a.columnName} != ''">
				${a.columnName}
				<#if a_has_next>
					,
				</#if >
			</if>
		</#list>	
	 ) 
	values (
		<#list attrs as a>
			<if test="${a.columnName} != null and ${a.columnName} != ''">
				${r'${'}${a.columnName},jdbcType=${a.columnType}${r'}'}
				<#if a_has_next>
					,
				</#if>
			</if>
		</#list>
	)
</insert>

<update id="update" parameterType="Object">
	update ${tableName}
	<set>
		<#list attrs as a>
			<#if a.primary?contains("FALSE")>
				<if test="${a.columnName} != null and ${a.columnName} != ''">
					${a.columnName} = ${r'${'}${a.columnName},jdbcType=${a.columnType}${r'}'}
				</if>
			</#if> 
		</#list>
	</set>
	<where>
		<#list attrs as a>
			<#if a.primary?contains("TRUE")>
				${a.columnName}=${r'${'}${a.columnName}${r'}'}
			</#if> 
		</#list>
	</where>
</update>
</mapper>   
